<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100px;height: 100px;background: pink;position: absolute;left: 0;top: 0;
        }
        .line{
            width: 3px;height: 500px;background: black;position: absolute;left: 500px;top: 0;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="line"></div>
</body>
<script>
    const obox=document.querySelector(".box")

    obox.onclick=function(){
        move(obox,{left:400,top:400})
    }

    function move(ele,date){
        clearInterval(ele.timer)
        ele.timer=setInterval(() => {
            let flag=true;                  //标记
            for(let i in date){             // i是属性，attr是值
                let now=parseInt(getStyle(ele,i));      //获取元素当前的值，与目标值作比较
                let speed=(date[i]-now)/7;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                
                if(date[i]!==now){
                    flag=false;
                }

                ele.style[i]=speed+now+"px";
            }
            if(flag){
                clearInterval(ele.timer)
            }
        }, 30);

    }


    function getStyle(ele, attr){
        if(getComputedStyle){
            return getComputedStyle(ele)[attr];
        }else{
            return ele.currentStyle[attr];
        }
    }
</script>
</html>